import { mapState, mapGetters } from 'vuex'
import { getUserInfo } from '@/api/modules/base.js'

let loading = false; // 防止多个组件共同引入触发多个请求
export default {

  computed: {
    ...mapState({
      userInfo: state => state.userInfo,
      linkUser: state => state.linkUser,
    }),
    ...mapGetters(['hasLogin'])
  },

  async onShow() {
    this.initUserInfo();
  },

  methods: {
    async initUserInfo(){
      if (!this.userInfo.id && !loading) {
        loading = true
        const res = await getUserInfo().finally(() => loading = false);
        this.$store.commit('SET_USER_INFO', res.tbMemberInfo);
        this.$store.commit("SET_LINK_USER", res.person_is ? res.person_is : {})
        if(this.renderHook) this.renderHook();
        return 
      } 
      
      // 做个轮询问，等待结果放行
      const timer = async () => {
        while(!this.userInfo.tmb_id){
          await uni.$u.sleep(0)
        }
        return true
      };
      await timer();
    }
  },
}